
//LCS 03.主题空间
class Solution {
public:
    int largestArea(vector<string>& grid) {
        int n=grid.size(),m=grid[0].size();
        int ans=0,flag=1;
        int dx[]={0,0,1,-1};
        int dy[]={1,-1,0,0};
        function<int(int,int,char)> dfs=[&](int x,int y,char p)
        {
            int ret=1;
            grid[x][y]='9';
            for(int k=0;k<4;k++)
            {
                int a=x+dx[k];
                int b=y+dy[k];
                if(a<0||a>n-1||b<0||b>m-1||grid[a][b]=='0') flag=0;
                if(a>=0&&a<n&&b>=0&&b<m&&grid[a][b]==p) ret+=dfs(a,b,p);
            }
            return ret;
        };    
       
        for(int i=1;i<n-1;i++)
        {
            for(int j=1;j<m-1;j++)
            {
                if(grid[i][j]!='0'&&grid[i][j]<='5') 
                {
                    flag=1;
                    int tmp=dfs(i,j,grid[i][j]);
                    if(flag) ans=max(ans,tmp);
                }
            }
        }
        return ans;
    }
};